package cn.com.connected.service.impl;

import cn.com.connected.common.config.CurrentData;
import cn.com.connected.common.emnus.MsgType;
import cn.com.connected.common.emnus.StatusType;
import cn.com.connected.common.emnus.Whether;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import cn.com.connected.service.SysMessageService;
import static cn.com.connected.entity.SysMessageEntity.*;
import cn.com.connected.base.params.page.BaseSearchDto;
import cn.com.connected.base.params.page.PageParam;
import cn.com.connected.base.params.page.PageResult;
import cn.com.connected.base.utils.BeanCopyUtils;
import cn.com.connected.dto.search.SysMessageSearchDto;
import cn.com.connected.entity.SysMessageEntity;
import cn.com.connected.mapper.SysMessageMapper;
import cn.com.connected.vo.SysMessageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * 
 *
 * @author sms
 * @email sms@live.com
 * @date 2022-10-22 09:37:51
 */
@Slf4j
@Service("sysMessageService")
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessageEntity> implements SysMessageService {
    @Resource
    private SysMessageMapper sysMessageMapper;

    @Override
    public SysMessageVo sysMessageAdd(String content,String memberId,String relId, MsgType msgType) {
        SysMessageEntity entity = new SysMessageEntity();
        entity.setContent(content);
        entity.setMemberId(memberId);
        entity.setRelId(relId);
        entity.setMsgType(msgType.getValue());
        entity.setReadStatus(Whether.not.getValue());
        entity.setCreateTime(new Date());
        entity.setStatus(StatusType.t1.getValue());
        sysMessageMapper.insert(entity);

        return sysMessageInfo(entity.getId());
    }

    @Override
    public void setReadStatus(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
//        List<SysMessageEntity> entities = ids.stream().map(o->{
//            SysMessageEntity m = new SysMessageEntity();
//            m.setId(o);
//            m.setReadStatus(Whether.yes.getValue());
//            m.setMemberId(CurrentData.getMemberId());
//            return m;
//        }).collect(Collectors.toList());
//        this.updateBatchById(entities);


        LambdaUpdateWrapper<SysMessageEntity> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(SysMessageEntity::getMemberId,CurrentData.getMemberId());
        wrapper.in(SysMessageEntity::getId,ids);

        SysMessageEntity en = new SysMessageEntity();
        en.setReadStatus(Whether.yes.getValue());
        sysMessageMapper.update(en,wrapper);
    }

    @Override
    public SysMessageVo sysMessageInfo(String id) {
        SysMessageEntity entity = sysMessageMapper.selectById(id);
        return BeanCopyUtils.copy(entity,new SysMessageVo());
    }

    @Override
    public Long unreadCount() {
        LambdaQueryWrapper<SysMessageEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysMessageEntity::getStatus,StatusType.t1.getValue());
        wrapper.eq(SysMessageEntity::getMemberId,CurrentData.getMemberId());
        wrapper.eq(SysMessageEntity::getReadStatus,Whether.not.getValue());
        return sysMessageMapper.selectCount(wrapper);
    }

    @Override
    public void sysMessageDel(String id) {
        SysMessageEntity entity = sysMessageMapper.selectById(id);
        entity.setStatus(StatusType.t0.getValue());
        sysMessageMapper.updateById(entity);
    }

    @Override
    public PageResult<SysMessageVo> sysMessagePage(BaseSearchDto<SysMessageSearchDto> searchParams) {
        LambdaQueryWrapper<SysMessageEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysMessageEntity::getStatus,StatusType.t1.getValue());
        wrapper.eq(SysMessageEntity::getMemberId,CurrentData.getMemberId());

        PageParam pageParam = searchParams.getPageParam();
        PageHelper.startPage(pageParam.getPageIndex(), pageParam.getPageSize());
        SysMessageSearchDto searchDto = searchParams.getSearchOption();

        wrapper.orderByDesc(Arrays.asList(SysMessageEntity::getCreateTime));

        Page<SysMessageEntity> page = (Page)sysMessageMapper.selectList(wrapper);

        PageResult<SysMessageVo> pageResult = BeanCopyUtils.copyPageResult(page, SysMessageVo.class);

        if (CollectionUtils.isNotEmpty(pageResult.getResultData())){
            List<String> ids = pageResult.getResultData().stream().filter(o->o.getReadStatus().equals(Whether.not.getValue())).map(SysMessageVo::getId).collect(Collectors.toList());
            CompletableFuture.runAsync(()-> setReadStatus(ids));
        }

        return pageResult;
    }

}
